/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package util;

import conn.Conn;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Desarrollo
 */
public class Table {

    public Table() {

    }

    public static String getPopup(String sql, String fields, String putFunc, String putFields) {
        String pp = "";
        String[] aFields = fields.split(",");
        String[] aPutFields = putFields.split(",");

        try {
            Statement stmt = Conn.getConn().createStatement();

            ResultSet rst = stmt.executeQuery(sql);
            int c, nc = aFields.length, p, np = aPutFields.length;
            
            while (rst.next())  {
                String put = "";
                for (p = 0; p < np; p ++) {
                    put += "'" + rst.getString(aPutFields[p]) + "', ";
                }
                put = put.substring(0, put.length() - 2);

                pp +=   "<tr>";
                for (c = 0; c < nc; c ++) {
                    String val = rst.getString(aFields[c]);
                     pp +=  "<td class=\"td-" + aFields[c] + (c == 0 ? " td-butt" : "") + "\">";
                     if (c == 0) pp +=  "<a href=\"JavaScript:;\" title=\"Insertar\" onclick=\"" + putFunc + "(" + put + ")\"><span>" + val + "</span></a>";
                     else pp += val;
                     pp +=  "</td>";
                }
                pp +=   "</tr>";
            }
        } catch (SQLException ex) {
            Logger.getLogger(Table.class.getName()).log(Level.SEVERE, null, ex);
        }

        return pp;
    }

    public static String getTable(String sql, String fields, String editFunc, String editFields, String delFunc, String delFields) {
        return getTable(sql, fields, editFunc, editFields, delFunc, delFields, false);
    }

    public static String getTable(String sql, String fields, String editFunc, String editFields, String delFunc, String delFields, boolean report) {
        String tbl = "";
        try {
            String[] aFields = fields.split(",");
            String[] aEditFields = editFields.split(",");
            String[] aDelFields = delFields.split(",");

            Statement stmt = Conn.getConn().createStatement();
            ResultSet rst = stmt.executeQuery(sql);
            while (rst.next())  {
                tbl +=	"<tr>";

                for (int f = 0; f < aFields.length; f ++) {
                    tbl +=  "<td class=\"td-" + aFields[f] + "\">" + rst.getString(aFields[f]) + "</td>";
                }

                String eParams = "";
                for (int e = 0; e < aEditFields.length; e ++) {
                    String eParam = aEditFields[e];
                    if (!eParam.equals("this")) eParam = "'" + rst.getString(eParam) + "'";
                    eParams += eParam + ", ";
                }
                eParams = eParams.substring(0, eParams.length() - 2);

                String dParams = "this, ";
                for (int d = 0; d < aDelFields.length; d ++) {
                    String dParam = aDelFields[d];
                    dParams += "'" + rst.getString(dParam) + "', ";

                }
                dParams = dParams.substring(0, dParams.length() - 2);

                if (!report) {
                    tbl +=	"<td class=\"td-butt\">" +
								"<a href=\"JavaScript:;\" title=\"Modificar\" onclick=\"" + editFunc + "(" + eParams + ");\">" +
									"<img src=\"imgs/page_white_edit.png\" alt=\"Modificar\">" +
								"</a>" +
							"</td>" +
							"<td class=\"td-butt\">" +
								"<a href=\"JavaScript:;\" title=\"Eliminar\" onclick=\"" + delFunc + "(" + dParams + ");\">" +
									"<img src=\"imgs/button_cancel.png\" alt=\"Eliminar\">" +
								"</a>" +
							"</td>";
                }
				tbl +=	"</tr>";
            }
        } catch (SQLException ex) {
            Logger.getLogger(Table.class.getName()).log(Level.SEVERE, null, ex);
        }

        return tbl;
    }
}